import isFunction from "./isFunction";
import isfunctionComponent from "./isfunctionComponent";
import mountNativElement from "./mountNativElement"
export default function mountComponent (virtualDom , container, oldDom) {
  let nextVirtualDom = null
  let component = null
  if (isfunctionComponent(virtualDom)) {// 函数组件
    nextVirtualDom = buildFunctionComponent(virtualDom)
  } else { // 类组件
    nextVirtualDom = buildClassComponent(virtualDom)
    component = nextVirtualDom.component
  }
  if (component) {
    component.componentDidMount()
    if (component.props && component.props.ref) {
      component.props.ref(component)
    }
  }
  if (isFunction(nextVirtualDom)) {
    mountComponent(nextVirtualDom, container, oldDom)
  } else {
    mountNativElement(nextVirtualDom, container, oldDom)
  }
}

function buildClassComponent (virtualDom) {
  const component = new virtualDom.type(virtualDom.props || {})
  const nextVirtualDom = component.render()
  nextVirtualDom.component = component
  return nextVirtualDom
}
function buildFunctionComponent (virtualDom) {
  // 运行函数组件方法，将componentVirtualDom 转换为普通virtualDom
  return virtualDom.type(virtualDom.props || {})
}